package com.cet.recon.step;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 30952
 */
@Component
@StepScope
public class PublishTasklet implements Tasklet {

    private final RabbitTemplate rabbitTemplate;

    @Value("#{jobParameters['run.date']}")
    private String runDate;

    public PublishTasklet(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
        // 构造一个简单的消息
        Map<String, Object> payload = new HashMap<>();
        payload.put("runDate", runDate);
        payload.put("status", "SUCCESS");

        // ★ 关键：用 convertAndSend，不要用 convertSendAndReceive
        rabbitTemplate.convertAndSend(
                "recon.exchange",          // 你声明的 exchange
                "recon.daily.summary",     // routing key
                payload
        );

        return RepeatStatus.FINISHED;
    }
}
